import { GetAvatarList, ChangeAvatar } from "~/api";

export const useContent = () => {
    const userStore = useUserStore();
    const list = ref([]);
    const activeIndex = ref("");
    const dialogVisible = ref(false);

    const GetList = async () => {
        const res = await GetAvatarList();
        list.value = res.data;
    };

    const onActivated = (index) => {
        activeIndex.value = index;
    };

    const onUpdate = async () => {
        if (activeIndex.value !== "") {
            const formData = new FormData();
            formData.append(
                "avatar",
                list.value[activeIndex.value].avatar_image
            );
            formData.append("type", 2);
            const res = await ChangeAvatar(formData);
            if (res.code == 200) {
                GET_AVATAR();
                Message().success("修改成功");
            } else {
                Message().error(res.msg);
            }
        } else {
            Message().warning("请选择一个头像");
        }
    };

    const onClose = () => {
        dialogVisible.value = false;
    };

    return {
        dialogVisible,
        activeIndex,
        list,
        onUpdate,
        onClose,
        onActivated,
        GetList,
    };
};
